package 华为OD机试真题2023.基础题;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class HJ19_寻找链表的中间结点 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/基础题/input/19.txt"));
        String start;
        int N;
        String[] sArray = in.nextLine().split(" ");
        start = sArray[0];
        N = Integer.parseInt(sArray[1]);
        Map<String, Integer> Date = new HashMap<>();
        Map<String, String> Next = new HashMap<>();
        for (int i = 0; i < N; i++) {
            sArray = in.nextLine().split(" ");
            String now = sArray[0];
            Integer date = Integer.parseInt(sArray[1]);
            String next = sArray[2];
            Date.put(now, date);
            Next.put(now, next);
        }
        String p = start;
        String pNext = Next.get(p);
        while (!"-1".equals(Next.get(p)) && !"-1".equals(Next.get(Next.get(p)))) {
            p = Next.get(p);
            pNext = Next.get(Next.get(pNext));
        }
        System.out.println(Date.get(p));
    }
}
